//
//  randomtestcard.c
//  Tests remodelCard() with random inputs
//
//  Created by Preston Skaggs on 2/6/14.
//
//

#include <stdio.h>
#include <stdlib.h>
#include "dominion.h"
#include "dominion_helpers.h"
#include "rngs.h"
#include "interface.h"
#include <math.h>
#include <assert.h>
#include <time.h>

#define CARD_FUNCTION 0
#define NUM_TESTS 2000

double randGen();
int randOne();
double randGen(){
    return  (double)rand()/(double)RAND_MAX;
}

int randOne(){
    double check = rand();
    if(check>(RAND_MAX/2)) return 1;
    else return 0;
}
int main(){
    srand(time(NULL));
    struct gameState *state = malloc(sizeof(struct gameState));
    assert(state != 0);
    int numPlayers, choice1, choice2, choice3, handPos, n, init, test, player;
    enum CARD first = adventurer;
    enum CARD last = treasure_map;
    enum CARD card = remodel;
    int bonus = 0;
    int randomSeed = 3;
    int kingdomCards[NUM_K_CARDS];
    
    
    if(CARD_FUNCTION == 0){
        for(n=0; n<=NUM_TESTS; n++){
            selectKingdomCards(randomSeed, kingdomCards);
            numPlayers = floor(randGen()*(MAX_PLAYERS+1));
            init = initializeGame(numPlayers, kingdomCards, randomSeed, state);
            if(init != 0){
                printf("Initialization failed  numPlayers = %d\n", numPlayers);
                continue;
            }
            player = floor(randGen()*numPlayers);
            state->whoseTurn = player;
            state->deckCount[player] = floor(randGen()*MAX_DECK+1);
            state->discardCount[player] = floor(randGen()*MAX_DECK+1);
            state->handCount[player] = floor(randGen()*MAX_HAND+1);
            handPos = (randGen()*MAX_HAND+1);
            choice1 = (randGen() *(last-first)+1)+first;
            choice2 = (randGen() *(last-first)+1)+first;
            test = randOne();
            printf("test value %d\n", test);
            if(test == 1){
                //this adds some randomness to testing if the cost of choice 1 is greater than choice2
                state->hand[player][choice1]= getCost(choice2)+floor(randGen());
            }
            printf("test %d  player = %d deckCount %d\n ", n, player, state->deckCount[player]);
            test = remodelCard(player, choice1, choice2, handPos, state);
            if(test != 0){
                printf("test %d failed\n", n);
            }
        }
        printf("Tests OK\n");
        
    }
    else{
        for(n=0; n<=NUM_TESTS; n++){
            //This test isn't very effective because drawnTreasure is always = 0 when adventurerCard() is called from cardEffect()
            //Also, the random generation of players isn't working.
            selectKingdomCards(randomSeed, kingdomCards);
            numPlayers = floor(randGen()*(MAX_PLAYERS+1));
            init = initializeGame(numPlayers, kingdomCards, randomSeed, state);
            if(init != 0){
                printf("Initialization failed  numPlayers = %d\n", numPlayers);
                continue;
            }
            player = floor(randGen()*numPlayers+1);
            state->whoseTurn = player;
            state->deckCount[player] = floor(randGen()*MAX_DECK+1);
            state->discardCount[player] = floor(randGen()*MAX_DECK+1);
            state->handCount[player] = floor(randGen()*MAX_HAND+1);
            handPos = (randGen()*MAX_HAND+1);
            choice1 = (randGen() *(last-first+1))+first;
            choice2 = (randGen() *(last-first+1))+first;
            choice3 = (randGen() *(last-first+1))+first;
            test = cardEffect(card, choice1, choice2, choice3, state, handPos, &bonus);
            if(test != 0){
                printf("test %d failed", n);
            }
        }
         printf("Tests OK\n");
    }
   
    return 0;
}